package com.coco.goods.controller;

import com.coco.goods.pojo.TbShopping;
import com.coco.goods.service.ShoppingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Package: com.coco.goods.controller
 * @Author: 张dy
 * @Date: Created in 2021/8/7 上午 11:21
 */
@RestController
@RequestMapping("/shopping")
public class ShoppingController {

    @Autowired
    private ShoppingService shoppingService;

    /**
     * 添加购物车
     * @Author: 张dy
     * @Date: 2021/8/8
     * @Param: [shoppings]
     */
    @PostMapping("/addShopping")
    public void addShopping(@RequestBody List<TbShopping> shoppings) {
        try {
            shoppingService.addShopping(shoppings);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 查看购物车
     * @Author: 张dy
     * @Date: 2021/8/8
     * @Param: [userId]
     */
    @GetMapping("/findByUserId/{userId}")
    public List<TbShopping> addShopping(@PathVariable("userId") Long userId) {
        return shoppingService.findByUserId(userId);
    }


    /**
     * 批量删除
     * @Author: 张dy
     * @Date: 2021/8/8
     * @Param: [userId, goodsId]
     */
    @DeleteMapping("/delete/{userId}")
    public void delete(@PathVariable("userId") Long userId,
                       @RequestParam("goodsId") Long[] goodsId) {
        try {

            shoppingService.delete(userId, goodsId);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
